package com.bankscene.screenfit.core;

import android.app.Activity;

import com.bankscene.screenfit.util.Stack;

/**
 * Created by zhaojin on 6/24/16.
 */

public class ActivityStackManager {

    private Stack<Activity> mActivityList;

    private static ActivityStackManager instance;

    private ActivityStackManager() {
    }

    public static ActivityStackManager newInstance() {
        if (instance == null) {
            instance = new ActivityStackManager();
            instance.mActivityList = new Stack<>();
        }
        return instance;
    }

    public void push(Activity activity) {
        mActivityList.push(activity);
    }

    public Activity pop() {
        Activity activity = null;
        if (!mActivityList.empty()) {
            activity = mActivityList.pop();
            activity.finish();
        }
        return activity;
    }

    public Activity peek() {
        return mActivityList.peek();
    }

    private void finishAllActivity(){
        while(!mActivityList.empty()) {
            Activity activity = mActivityList.pop();
            activity.finish();
        }
    }

    public void appExit() {
        try {
            finishAllActivity();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
